package com.viewpagertext.utils;


import android.util.Log;

import com.viewpagertext.DBHelper.AppDatabase;
import com.viewpagertext.DBHelper.Dao.DownloadMusicDao;
import com.viewpagertext.DBHelper.Dao.StarredSongDao;
import com.viewpagertext.Model.DownloadMusic;

import java.io.File;

public class DownloadMusicUtils {
    private static StarredSongDao starredSongDao;
    public static void downloadMusicMethod(String url, String pic, DownloadMusic downloadMusic){
        new FileDownloader() {
            @Override
            protected void onPostExecute(String result) {
                System.out.println(result);
                if (result.startsWith("/")) {
                    // 下载成功
                    DownloadMusicDao dao = AppDatabase.getInstance().downloadMusicDao();
                    System.out.println("result：" + result);
                    String []result_path = result.split("#@!");
//                    System.out.println("wwwwwww" + result_path);
                    System.out.println("kkkkkkkkk"+result_path[0]);
                    downloadMusic.path = result_path[0];
                    if(!(new File(result_path[1]).exists())) downloadMusic.pic = "";
                    else downloadMusic.pic = result_path[1];

                    dao.insertDownloadMusic(downloadMusic);
                    System.out.println("insert" + result);
                    starredSongDao = AppDatabase.getInstance().starredSongDao();
                    if (starredSongDao.getStarredSongByNameAndSinger(downloadMusic.name, downloadMusic.singer) != null)
                        starredSongDao.updateByNameAndSinger(downloadMusic.name, downloadMusic.singer, downloadMusic.path);
                } else {
                    // 下载失败
                    System.out.println("下载失败");
                }
            }
        }.execute(url, pic, downloadMusic.name + "-" + downloadMusic.singer);

//        System.out.println("down list" + dao.getAllDownloadMusic().size());
//        System.out.println("down1" + dao.getAllDownloadMusic().get(1).time);
    }
}
